/* eslint-disable array-callback-return */
/**
 *  转换ant table的sorter对象转换成分页的查询的排序字段
 * @param {object} sorter
 */
export const conventSorter = sorter => {
    const option = {};
    if(sorter)
        if(sorter.order && sorter.field) {
            // 点击排序
            option.sortField = sorter.field;
            if(sorter.order === 'descend') option.isDesc = true;
            else option.isDesc = false;
        } else {
            // 取消排序
            option.sortField = undefined;
            option.isDesc = undefined;
        }
    return option;
};
export const hasPermissions = (data, name) => Array.isArray(data) && data.length > 0 && data.some(item => item === name);
/* eslint-disable prefer-const, no-bitwise, no-mixed-operators */

export const uuid = () =>
    'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
        let r = (Math.random() * 16) | 0,
            v = c === 'x' ? r : (r & 0x3) | 0x8;
        return v.toString(16);
    });
import {PERMISSION} from './constants';
export const isSubmitable = (data = [], options = [], url, id) => {
    if(!url) return false;
    if(id) return data.includes(PERMISSION[url]) && options.includes(PERMISSION[url]);
    return data.includes(PERMISSION[url]);
};
/**
 * 计算table的 selectedRows
 * @param {*} preKeys 上一个操作 返回的 selectedKeys
 * @param {*} preRows 上一个操作 返回的 selectedRows
 * @param {*} currentKeys 本次操作 返回的 selectedKeys
 * @param {*} currentRows 本次操作 返回的 selectedRows
 * @param {*} keyIndex eg: id
 */
import difference from 'lodash/difference';
export const computeSelectedRows = (preKeys, preRows, currentKeys, currentRows, keyIndex = 'salePackingPartListDetailId') => {
    if(currentKeys.length === 0)
        return [];
    // 勾选
    if(currentKeys.length > preKeys.length) {
        const newKeys = difference(currentKeys, preKeys);
        const newRows = [];
        newKeys.forEach(key => {
            const row = currentRows.find(r => r[keyIndex] === key);
            if(row)
                newRows.push(row);
        });
        return [...preRows, ...newRows];
    } else if(currentKeys.length < preKeys.length) { // 取消勾选
        const deleteKeys = difference(preKeys, currentKeys);
        const newRows = preRows.filter(d => !deleteKeys.includes(d[keyIndex]));
        return newRows;
    }
    return preRows;
};

export const ListTypeCL = data => {
    const text = [];
    if(data)
        data.map(item => {
            text.push({
                text: `${item.code}(${item.name})`,
                value: item.id
            });
        });
    return text;
};

export const dataTimeDay = (start, stop) => {
    let startTime = new Date(start).getTime();
    let stopTime = new Date(stop).getTime();
    // eslint-disable-next-line radix
    return start && stop ? Math.floor(parseInt(startTime - stopTime) / (1000 * 60 * 60 * 24)) : 0;
};
